package com.iotechn.unimall.admin.api.cmsRotation;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.Const;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.core.exception.ThirdPartServiceException;
import com.iotechn.unimall.core.notify.SMSClient;
import com.iotechn.unimall.core.notify.SMSResult;
import com.iotechn.unimall.core.util.GeneratorUtil;
import com.iotechn.unimall.core.util.MD5Util;
import com.iotechn.unimall.core.util.SHAUtil;
import com.iotechn.unimall.data.component.CacheComponent;
import com.iotechn.unimall.data.domain.CmsRotationDO;
import com.iotechn.unimall.data.mapper.CmsRotationMapper;
import com.iotechn.unimall.data.model.Page;
import com.iotechn.unimall.data.util.SessionUtil;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


@Service
public class CmsRotationServiceImpl implements CmsRotationService {
    @Autowired
    private CmsRotationMapper cmsRotationMapper;

     @Override
        public Page<CmsRotationDO> page(Integer status,Integer page, Integer limit, Long adminId) throws ServiceException {
            Wrapper<CmsRotationDO> wrapper = new EntityWrapper<CmsRotationDO>();
            wrapper.orderBy("id", false);
             if(status != null){
                 wrapper.eq("status",status);
             }
            Integer count = cmsRotationMapper.selectCount(wrapper);
            List<CmsRotationDO> CmsRotationDOS = cmsRotationMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
            return new Page<>(CmsRotationDOS, page, limit, count);
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public CmsRotationDO create(CmsRotationDO cmsRotationDO, Long adminId) throws ServiceException {
            Date now=new Date();
            cmsRotationDO.setGmtCreate(now);
            cmsRotationDO.setGmtUpdate(now);
            if (cmsRotationMapper.insert(cmsRotationDO) > 0) {
                cmsRotationDO.setId(cmsRotationDO.getId());
                return cmsRotationDO;
            }
             throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public String update(CmsRotationDO cmsRotationDO, Long adminId) throws ServiceException {
            Long id = cmsRotationDO.getId();
            if (id == null) {
               return "fail";
            }
            Date now=new Date();
            cmsRotationDO.setGmtUpdate(now);
            if (cmsRotationMapper.updateById(cmsRotationDO) > 0) {
                return "ok";
            }
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
     }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (cmsRotationMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

     }

    @Override
    public CmsRotationDO detail(Long id, Long adminId) throws ServiceException {
       return cmsRotationMapper.selectById(id);
    }

}